package com.punai.monthanalysis.sqliteDao;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.common.utils.sqlite.SqliteUtil;
import com.punai.common.utils.sqliteConne.StandardUserTemplConnection;
import com.punai.monthanalysis.sqlitedomain.CostStructureCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConfigurationProperties(prefix = "sqlite")
public class CostStructureCategoryRepository {
    private static SqliteUtil sqliteUtil;

    private String userTempl;

    private void initConnection() {
        sqliteUtil = new SqliteUtil();
        if (sqliteUtil.isConnNull()) {
            sqliteUtil.getConnection(userTempl);
        }
    }

    public CostStructureCategory findAllById(String id){
        initConnection();
        String sql="SELECT * from CostStructureCategory where 1=1";
        if(StrUtil.isEmpty(id)){
            return null;
        }
        sql+=" and ID ='"+id+"'";
        return sqliteUtil.selectOneRecord(CostStructureCategory.class, sql);
    }


    public List<CostStructureCategory> findAll(){
        initConnection();
        String sql="SELECT * from CostStructureCategory";
        return sqliteUtil.selectListRecord(CostStructureCategory.class, sql);
    }



    public List<CostStructureCategory> findAllByIdIn(List<String> ids){
        initConnection();
        String sql="SELECT * from CostStructureCategory where 1=1";
        if(CollUtil.isEmpty(ids)){
            return null;
        }
        sql+=" and ID IN('" + StrUtil.join("','",ids) +"')";
        return sqliteUtil.selectListRecord(CostStructureCategory.class, sql);
    }


    public List<CostStructureCategory> findAllByCodeIn(List<String> codes){
        initConnection();
        String sql="SELECT * from CostStructureCategory where 1=1";
        if(CollUtil.isEmpty(codes)){
            return null;
        }
        for (String code : codes) {
            sql+=" and CategoryCode like '" +  code +"%' ";
        }
        return sqliteUtil.selectListRecord(CostStructureCategory.class, sql);
    }


    public String getUserTempl() {
        return userTempl;
    }

    public void setUserTempl(String userTempl) {
        this.userTempl = userTempl;
    }
}
